package com.nbgs.demo.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbgs.demo.model.Worker;
import org.apache.ibatis.annotations.*;

public interface WorkerMapper {
    //根据条件展示worker信息
    @Select("<script>\n"+
            "SELECT\n" +
            "worker_num,\n" +
            "worker_state,\n" +
            "worker_name,\n" +
            "worker_gender,\n" +
            "worker_address,\n" +
            "worker_id_num,\n" +
            "worker_tel_num,\n" +
            "worker_start_time\n" +
            "FROM\n" +
            "t_worker\n" +
            "<where>\n" +
            "<if test=\"workerName!=null and workerName!=''\">\n" +
            "and worker_name like CONCAT('%',#{workerName},'%')\n" +
            "</if>\n" +
            "<if test=\"workerNum!=null and workerNum!=''\">\n" +
            "and worker_num LIKE CONCAT('%',#{workerNum},'%')\n" +
            "</if>\n" +
            "<if test=\"workerState!=null and workerState!=''\">\n" +
            "and worker_state=#{workerState}\n" +
            "</if>\n" +
            "</where>\n" +
            "ORDER BY worker_num DESC\n"+
            "</script>\n")
     IPage<Worker> showSumWorker(Page<Worker> page,@Param(value="workerName")String workerName,@Param(value="workerNum")String workerNum,@Param(value="workerState")String workerState);
     //查询满足条件的总条数
    @Select("<script>\n"+
            "SELECT\n" +
            "count(worker_num)\n" +
            "FROM\n" +
            "t_worker\n" +
            "<where>\n" +
            "<if test=\"workerName!=null and workerName!=''\">\n" +
            "and worker_name like CONCAT('%',#{workerName},'%')\n" +
            "</if>\n" +
            "<if test=\"workerNum!=null and workerNum!=''\">\n" +
            "AND worker_num LIKE CONCAT('%',#{workerNum},'%')\n" +
            "</if>\n" +
            "<if test=\"workerState!=null and workerState!=''\">\n" +
            "AND worker_state=#{workerState}\n" +
            "</if>\n" +
            "</where>\n" +
            "ORDER BY worker_num DESC\n"+
            "</script>\n")
    int totalWorker(@Param(value="workerName")String workerName,@Param(value="workerNum")String workerNum,@Param(value="workerState")String workerState);


    //录入员工信息
    @Options(useGeneratedKeys=true, keyProperty="workerId", keyColumn="worker_id")
    @Insert("insert INTO t_worker(worker_num,worker_password,worker_state,worker_name,worker_gender,worker_address,worker_id_num,worker_tel_num,worker_start_time)\n" +
            "VALUE (#{workerNum},#{workerPassword},#{workerState},#{workerName},#{workerGender},#{workerAddress},#{workerIdNum},#{workerTelNum},#{workerStartTime})")
    void addWorker(Worker worker);

   //修改员工的信息
    @Update("<script>\n"+
            "UPDATE t_worker \n" +
            "<set>\n" +
            "<if test=\"workerPassword!=null and workerPassword!=''\">\n" +
            "worker_password=#{workerPassword},\n" +
            "</if>\n" +
            "<if test=\"workerState!=null and workerState!=''\">\n" +
            "worker_state=#{workerState},\n" +
            "</if>\n" +
            "<if test=\"workerTelNum!=null and workerTelNum!=''\">\n" +
            "worker_tel_num=#{workerTelNum},\n" +
            "</if>\n" +
            "</set>\n" +
            "WHERE\n" +
            "worker_num=#{workerNum}\n"+
            "</script>\n")
    void updateWorker(Worker worker);

   //录入员工角色关联表
    @Insert("insert into t_worker_role(worker_id,role_id) value(#{workerId},(SELECT role_id FROM t_role where role_name=#{roleName}))")
    void addRoleAndRole(@Param(value="workerId") int workerId,@Param(value="roleName") String roleName);
}
